<?php
declare (strict_types = 1);

namespace app\controller\user\v1;

use app\BaseController;
use think\facade\Db;

class Mine extends BaseController
{
    /**
     * 获取用户信息和菜单列表
     * @return void
     */
    public function getInfo()
    {
        try {
            $uuid = $this->request->uuid;
            
            // 检查必要参数
            if (empty($uuid)) {
                return result(FAIL, '缺少用户标识');
            }
            
            // 查询用户信息
            $userInfo = Db::table('wxappUser')
                ->where('uuid', $uuid)
                ->where('isDelete', 0)
                ->field('avatarUrl , nickname , cellPhone , region, corpName, business, account, address, corpImage')
                ->find();
                
            if (empty($userInfo)) {
                return result(FAIL, '用户不存在');
            }
            $region = explode(',', $userInfo['region']);
            $userInfo['region'] = end($region);

            // 定义菜单列表
            $menuList = [
                [
                    'icon' => '/resource/image/about.png',
                    'text' => '关于我们',
                    'type' => 'about'
                ],
                [
                    'icon' => '/resource/image/contact.png',
                    'text' => '联系客服',
                    'type' => 'contact'
                ],
                [
                    'icon' => '/resource/image/logout.png',
                    'text' => '退出登录',
                    'type' => 'logout'
                ]
            ];
            // 获取当前域名     
            $domain = request()->domain();
            $contactInfo = [
                'wechatQR'=> $domain.'/images/wechat.png',
                'publicQR'=> $domain.'/images/public.png',
                'phone'=> [
                  '132-5038-8148',
                  '134-1101-0301',
                ],
                'time'=>'09:00-17: 00',
            ];
            // 返回数据
            $result = [
                'info' => $userInfo,
                'menuList' => $menuList,
                'contactInfo'=>$contactInfo,
            ];
            
            return result(SUCCESS, '', $result);
        } catch (\Exception $e) {
            return result(FAIL, '系统错误：' . $e->getMessage());
        }
    }

    /**
     * 保存用户信息
     * @return void
     */
    public function save()
    {
        try {
            $uuid = $this->request->uuid;
            $data = $this->request->data;
            
            // 检查必要参数
            if (empty($uuid)) {
                return result(FAIL, '缺少用户标识');
            }
            // 准备要更新的字段
            $updateData = [];
            // 可更新的字段列表
            $allowedFields = [
                'nickname', 'avatarUrl', 'corpName', 
                'business', 'account', 'address', 'industry', 
                'corpImage', 'region'
            ];
            
            // 过滤要更新的字段
            foreach ($allowedFields as $field) {
                if (isset($data[$field])) {
                    $updateData[$field] = $data[$field];
                }
            }
            $updateData['region'] = implode(',', $data['region']);
            if (empty($updateData)) {
                return result(FAIL, '没有可更新的数据');
            }
            
            // 更新用户信息
            $result = Db::table('wxappUser')
                ->where('uuid', $uuid)
                ->update($updateData);
                
            if ($result !== false) {
                return result(SUCCESS, '保存成功');
            } else {
                return result(FAIL, '保存失败');
            }
        } catch (\Exception $e) {
            return result(FAIL, '系统错误：' . $e->getMessage());
        }
    }
    /**
     * 获取用户详细信息
     * @return void
     */
    public function getDetail()
    {
        try {
            $uuid = $this->request->uuid;
            
            // 检查必要参数
            if (empty($uuid)) {
                return result(FAIL, '缺少用户标识');
            }
            
            // 查询用户信息
            $userInfo = Db::table('wxappUser')
                ->where('uuid', $uuid)
                ->where('isDelete', 0)
                ->field('avatarUrl, nickname , cellPhone , region, corpName, business, account, address, corpImage,industry')
                ->find();

            // region用逗号分割成数组
            $userInfo['region'] = explode(',', $userInfo['region']);
                
            if (empty($userInfo)) {
                return result(FAIL, '用户不存在');
            }
            
            return result(SUCCESS, '获取成功', $userInfo);
        } catch (\Exception $e) {
            return result(FAIL, '系统错误：' . $e->getMessage());
        }
    }
} 